import axios from "axios";
import { MetroStation, City } from "../db";

axios.get("https://api.hh.ru/metro").then(async (res) => {
    await City.truncate({ cascade: true });
    await MetroStation.truncate({ cascade: true });

    console.log(res.data);
    res.data.map(async (city) => {
        const cityData = await City.create({
            name: city.name,
        });
        const stations = city.lines
            .map((line) => [...line.stations])[0]
            .map((station) => {
                console.log(station);
                return {
                    cityId: cityData.id,
                    name: station.name,
                    line: {
                        hexColor: station.line.hex_color,
                        name: station.line.name,
                    },
                };
            });
        MetroStation.bulkCreate(stations);
    });
    console.log("[PARSE] Metro completed");
});
